package com.lyhengtongwl.zqsnews.widget;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lyhengtongwl.zqsnews.R;
import com.lyhengtongwl.zqsnews.entity.TJTopScrollEntity;

import java.util.List;

public class ScrollTextView extends LinearLayout {
//    private TextView mBannerTV1;
//    private TextView mBannerTV2;

    private LinearLayout banner1;

    private LinearLayout banner2;
    private TextView tvState1;
    private TextView tvContent1;
    private TextView tvMoney1;
    private TextView tvState2;
    private TextView tvContent2;
    private TextView tvMoney2;

    private TextView tvDw1;
    private TextView tvDw2;
    private Handler handler;
    private boolean isShow = false;
    private int startY1, endY1, startY2, endY2;
    private Runnable runnable;
    private List<TJTopScrollEntity> list;
    private int position = 0;
    private int offsetY = 100;
    private boolean hasPostRunnable = false;
    private View view;

    public ScrollTextView(Context context) {
        this(context, null);
    }

    public ScrollTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    //初始化组件
    private void initView() {
        banner1 = view.findViewById(R.id.ll_banner);
        banner2 = view.findViewById(R.id.ll_banner1);


        tvState1 = view.findViewById(R.id.tv_state);
        tvContent1 = view.findViewById(R.id.tv_content);

        tvMoney1 = view.findViewById(R.id.tv_money);


        tvState2 = view.findViewById(R.id.tv_state1);
        tvContent2 = view.findViewById(R.id.tv_content1);

        tvMoney2 = view.findViewById(R.id.tv_money1);


        tvDw1 = view.findViewById(R.id.tv_dw);
        tvDw2 = view.findViewById(R.id.tv_dw1);
    }

    public ScrollTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        view = LayoutInflater.from(context).inflate(R.layout.widget_scroll_text_layout, this);
        initView();

        handler = new Handler();
        runnable = new Runnable() {
            @Override
            public void run() {
                isShow = !isShow;
                if (position >=list.size() - 1) {
                    position = 0;
                }
                position=position+1;
                if (isShow) {
                    tvState1.setText(list.get(position).getState());
                    tvContent1.setText(list.get(position).getContent());
                    tvMoney1.setText(list.get(position).getMoney());

                    tvState2.setText(list.get(position-1).getState());
                    tvContent2.setText(list.get(position-1).getContent());
                    tvMoney2.setText(list.get(position-1).getMoney());
                    tvDw1.setText("元");
                    tvDw2.setText("元");
                } else {
                    tvState1.setText(list.get(position-1).getState());
                    tvContent1.setText(list.get(position-1).getContent());
                    tvMoney1.setText(list.get(position-1).getMoney());

                    tvState2.setText(list.get(position).getState());
                    tvContent2.setText(list.get(position).getContent());
                    tvMoney2.setText(list.get(position).getMoney());
                    tvDw1.setText("元");
                    tvDw2.setText("元");
                }
                startY1 = isShow ? 0 : offsetY;
                endY1 = isShow ? -offsetY : 0;
                ObjectAnimator.ofFloat(banner1, "translationY", startY1, endY1).setDuration(300).start();

                startY2 = isShow ? offsetY : 0;
                endY2 = isShow ? 0 : -offsetY;
                ObjectAnimator.ofFloat(banner2, "translationY", startY2, endY2).setDuration(300).start();

                handler.postDelayed(runnable, 3000);
            }
        };
    }

    public List<TJTopScrollEntity> getList() {
        return list;
    }

    public void setList(List<TJTopScrollEntity> list) {
        this.list = list;

        //处理最后一条数据切换到第一条数据 太快的问题
        if (list.size() > 1) {
            list.add(list.get(0));
        }
    }

    public void startScroll() {
        tvState1.setText(list.get(0).getState());
        tvContent1.setText(list.get(0).getContent());
        tvMoney1.setText(list.get(0).getMoney());
        tvDw1.setText("元");
        if (list.size() > 1) {
            if (!hasPostRunnable) {
                hasPostRunnable = true;
                //处理第一次进入 第一条数据切换第二条 太快的问题
                handler.postDelayed(runnable, 3000);
            }
        } else {
            //只有一条数据不进行滚动
            hasPostRunnable = false;
//            mBannerTV1.setText(list.get(0));
        }
    }

    public void stopScroll() {
        handler.removeCallbacks(runnable);
        hasPostRunnable = false;
    }
}
